//
//  ZHMainController.swift
//  weibosina
//
//  Created by zhangzhen on 16/6/3.
//  Copyright © 2016年 zhangzhen. All rights reserved.
//

import UIKit

class ZHMainController: UITabBarController {
    //绑定按钮
    var btn : UIButton?
    

    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildControllers()
        //添加设置按钮
        addSetUpButton()

    }




override func viewWillAppear(animated: Bool) {
    // 会创建 tabBar 中的所有控制器对应的按钮！
    super.viewWillAppear(animated)
    
    // 将撰写按钮弄到最前面
    tabBar.bringSubviewToFront(btn!)
}

//实现按钮点击的方法
    func didClickSetUpButton()  {
    print("点我了")
 }
    
    
}

//MARK - 设置界面
extension ZHMainController{
    
    //添加一个按钮
    private func addSetUpButton(){
        //1.创建按钮
        let btn = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
        print("....")
        self.btn = btn
       
        //2.添加按钮
        tabBar.addSubview(btn)
        //3.设置button的frame
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 2;
        btn.frame = CGRectInset(tabBar.bounds, 2*w, 0)
        //添加点击事件
        btn .addTarget(self, action: Selector("didClickSetUpButton"), forControlEvents: .TouchUpInside)
        
        
        
    }
    
    //添加多个控制器
    private func addChildControllers(){
        tabBar.tintColor = UIColor.orangeColor()
        //1.添加home控制器
        addChildController(ZHHomeController(), title: "主页", imageName: "tabbar_home")
        //2.添加message控制器
        addChildController(ZHMessageController(), title: "消息", imageName: "tabbar_message_center")
        //3.添加一个普通的控制器
        addChildViewController(UIViewController())
        //4.添加发现
        addChildController(ZHDiscoverController(), title: "发现", imageName: "tabbar_discover")
        //5.添加me
        addChildController(ZHMeController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    
    //添加一个子控制器
    private func addChildController(vc:UIViewController,title:String,imageName:String )  {
        //1.创建一个导航控制器
        let nav = UINavigationController(rootViewController: vc)
        //2.设置title,图片
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //3.添加到控制器中
        addChildViewController(nav)
        
    }
    
}
